package com.nayan.epic;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;
import android.widget.RemoteViews;

public class EpicUpdaterService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		updateWidget();
		return super.onStartCommand(intent, flags, startId);
	}
	
	private void updateWidget() {
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		ComponentName thisWidget = new ComponentName(this,
				EpicBroadcastReceiver.class);
		int[] widgetIds = manager.getAppWidgetIds(thisWidget);
		for (int widgetId : widgetIds) {
			RemoteViews remoteViews = new RemoteViews(getPackageName(),
					R.layout.main);
			resetViews(remoteViews);
			updateViews(remoteViews);
			manager.updateAppWidget(widgetId, remoteViews);
		}	
		
	}
	
	private void updateViews(RemoteViews remoteViews) {
		Time now = new Time();
		now.setToNow();
		remoteViews.setImageViewResource(R.id.imageViewit, R.drawable.it1);
		remoteViews.setImageViewResource(R.id.imageViewis, R.drawable.is1);
		int hour = now.hour;
		if(now.minute < 5) {
			remoteViews.setImageViewResource(R.id.imageViewoclock,
					R.drawable.oclock1);
		}
		else if (now.minute <= 34 && now.minute >= 5) {
			if (now.minute >= 30) {
				remoteViews.setImageViewResource(R.id.imageViewhalf,
						R.drawable.half1);
			} else {
				if(now.minute < 10) {
					remoteViews.setImageViewResource(R.id.imageViewfive1, R.drawable.five1);
				}
				else if(now.minute < 15) {
					remoteViews.setImageViewResource(R.id.imageViewten1, R.drawable.ten1);
				}
				else if(now.minute < 20) {
					remoteViews.setImageViewResource(R.id.imageViewtwenty, R.drawable.twenty1);
				}
				else {
					remoteViews.setImageViewResource(R.id.imageViewtwenty, R.drawable.twenty1);
					remoteViews.setImageViewResource(R.id.imageViewfive1, R.drawable.five1);
				}
				remoteViews.setImageViewResource(R.id.imageViewminutes,
						R.drawable.minutes1);
			}
			remoteViews.setImageViewResource(R.id.imageViewpast,
					R.drawable.past1);
		} else {
			
			if(now.minute < 40) {
				remoteViews.setImageViewResource(R.id.imageViewtwenty, R.drawable.twenty1);
				remoteViews.setImageViewResource(R.id.imageViewfive1, R.drawable.five1);
			}
			else if(now.minute < 45) {
				remoteViews.setImageViewResource(R.id.imageViewtwenty, R.drawable.twenty1);
			}
			else if(now.minute < 50) {
				remoteViews.setImageViewResource(R.id.imageViewten1, R.drawable.ten1);
			}
			else {
				remoteViews.setImageViewResource(R.id.imageViewfive1, R.drawable.five1);
			}
			
			remoteViews.setImageViewResource(R.id.imageViewminutes,
					R.drawable.minutes1);
			remoteViews.setImageViewResource(R.id.imageViewto,
					R.drawable.to1);
			hour++;
			if (hour == 24)
				hour = 1;
		}

		switch (hour) {
		case 1:
		case 13:
			remoteViews.setImageViewResource(R.id.imageViewone,
					R.drawable.one1);
			break;
		case 2:
		case 14:
			remoteViews.setImageViewResource(R.id.imageViewtwo,
					R.drawable.two1);
			break;
		case 3:
		case 15:
			remoteViews.setImageViewResource(R.id.imageViewthree,
					R.drawable.three1);
			break;
		case 4:
		case 16:
			remoteViews.setImageViewResource(R.id.imageViewfour,
					R.drawable.four1);
			break;
		case 5:
		case 17:
			remoteViews.setImageViewResource(R.id.imageViewfive2,
					R.drawable.five1);
			break;
		case 6:
		case 18:
			remoteViews.setImageViewResource(R.id.imageViewsix,
					R.drawable.six1);
			break;
		case 7:
		case 19:
			remoteViews.setImageViewResource(R.id.imageViewseven,
					R.drawable.seven1);
			break;
		case 8:
		case 20:
			remoteViews.setImageViewResource(R.id.imageVieweight,
					R.drawable.eight1);
			break;
		case 9:
		case 21:
			remoteViews.setImageViewResource(R.id.imageViewnine,
					R.drawable.nine1);
			break;
		case 10:
		case 22:
			remoteViews.setImageViewResource(R.id.imageViewten2,
					R.drawable.ten1);
			break;
		case 11:
		case 23:
			remoteViews.setImageViewResource(R.id.imageVieweleven,
					R.drawable.eleven1);
			break;
		case 12:
		case 0:
			remoteViews.setImageViewResource(R.id.imageViewtwelve,
					R.drawable.twelve1);
			break;

		}
	}

	private void resetViews(RemoteViews remoteViews) {
		remoteViews.setImageViewResource(R.id.imageViewit, R.drawable.it0);
		remoteViews.setImageViewResource(R.id.imageViewis, R.drawable.is0);
		remoteViews.setImageViewResource(R.id.imageViewhalf, R.drawable.half0);
		remoteViews.setImageViewResource(R.id.imageViewten1, R.drawable.ten0);
		remoteViews.setImageViewResource(R.id.imageViewquarter, R.drawable.quarter0);
		remoteViews.setImageViewResource(R.id.imageViewtwenty, R.drawable.twenty0);
		remoteViews.setImageViewResource(R.id.imageViewfive1, R.drawable.five0);
		remoteViews.setImageViewResource(R.id.imageViewminutes, R.drawable.minutes0);
		remoteViews.setImageViewResource(R.id.imageViewto, R.drawable.to0);
		remoteViews.setImageViewResource(R.id.imageViewpast, R.drawable.past0);
		remoteViews.setImageViewResource(R.id.imageViewone, R.drawable.one0);
		remoteViews.setImageViewResource(R.id.imageViewthree, R.drawable.three0);
		remoteViews.setImageViewResource(R.id.imageViewtwo, R.drawable.two0);
		remoteViews.setImageViewResource(R.id.imageViewfour, R.drawable.four0);
		remoteViews.setImageViewResource(R.id.imageViewfive2, R.drawable.five0);
		remoteViews.setImageViewResource(R.id.imageViewsix, R.drawable.six0);
		remoteViews.setImageViewResource(R.id.imageViewseven, R.drawable.seven0);
		remoteViews.setImageViewResource(R.id.imageVieweight, R.drawable.eight0);
		remoteViews.setImageViewResource(R.id.imageViewnine, R.drawable.nine0);
		remoteViews.setImageViewResource(R.id.imageViewten2, R.drawable.ten0);
		remoteViews.setImageViewResource(R.id.imageVieweleven, R.drawable.eleven0);
		remoteViews.setImageViewResource(R.id.imageViewtwelve, R.drawable.twelve0);		
		remoteViews.setImageViewResource(R.id.imageViewoclock, R.drawable.oclock0);

	}
	

}
